package net.neimeng.luculent.commonmodule.viewmodel;

import android.app.Application;
import android.arch.lifecycle.MutableLiveData;
import android.support.annotation.NonNull;

import net.neimeng.luculent.commonmodule.entities.Contacts;
import net.neimeng.luculent.commonmodule.service.AddressService;
import net.neimeng.luculent.coremodel.base.viewmodel.BaseViewModel;
import net.neimeng.luculent.coremodel.datamodel.http.ListResult;
import net.neimeng.luculent.coremodel.datamodel.http.RXObserver;
import net.neimeng.luculent.coremodel.datamodel.http.RetrofitUtils;

import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;

public class AddressTabViewModel extends BaseViewModel {

    private AddressService addressService = RetrofitUtils.getService(AddressService.class);

    public AddressTabViewModel(@NonNull Application application) {
        super(application);
    }

    //联系人数据集合
    private MutableLiveData<ListResult<Contacts>> contacts = new MutableLiveData<>();

    public MutableLiveData<ListResult<Contacts>> getContacts() {
        return contacts;
    }

    //获取所有联系人数据
    public void getAllContacts(){
        Observable<ListResult<Contacts>> observable =  addressService.getAllContacts(true);
        observable.compose(this.compose())
                .subscribe(new RXObserver<ListResult<Contacts>>() {
                    @Override
                    public void onSubscribe(Disposable disposable) {
                        addDisposable(disposable);
                    }
                    @Override
                    public void onNext(ListResult<Contacts> result) {
                        contacts.setValue(result);
                    }

                });
    }

}
